﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DA
{
    public class KhachHangDA: BaseConnection
    {
        public SqlDataAdapter danhSachKhachHangAdapter {set;get;}
        public KhachHangDA()
        {
            
        }
        public DataSet layDanhSachKhachHang()
        {
            openConnect();
            String sqlStm = "SELECT * FROM KhachHang";
            SqlCommand cmd = new SqlCommand(sqlStm, con);
            danhSachKhachHangAdapter = new SqlDataAdapter(cmd);
            danhSachKhachHangAdapter.SelectCommand = cmd;
            SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(danhSachKhachHangAdapter);
            DataSet dsKhachHang = new DataSet();
            danhSachKhachHangAdapter.Fill(dsKhachHang, "KhachHang");
            closeConnect();
            return dsKhachHang;        
        }
        public DataSet timKiemKhachHang(int timKiemTheo, String strchuoiTimKiem)
        {
            DataSet ds = new DataSet();
            String sqlStatement = "";
            if (timKiemTheo == 1)
            {
                sqlStatement = "SELECT MaKhachHang, TenKhachHang, DiaChiLienLac, SoDienThoai, LoaiKhachHang";
                sqlStatement += " FROM KhachHang";
                sqlStatement += " WHERE MaKhachHang = '" + strchuoiTimKiem + "'";

            }
            else if (timKiemTheo == 2)
            {
                sqlStatement = " SELECT MaKhachHang, TenKhachHang, DiaChiLienLac, SoDienThoai, LoaiKhachHang";
                sqlStatement += " FROM KhachHang";
                sqlStatement += " WHERE TenKhachHang like N'%" + strchuoiTimKiem + "%'";
            }
            else if (timKiemTheo == 3)
            {
                sqlStatement = " SELECT MaKhachHang, TenKhachHang, DiaChiLienLac, SoDienThoai, LoaiKhachHang";
                sqlStatement += " FROM KhachHang";
                sqlStatement += " WHERE DiaChiLienLac like N'%" + strchuoiTimKiem + "%'";
            }
            else if (timKiemTheo == 4)
            {
                sqlStatement = " SELECT MaKhachHang, TenKhachHang, DiaChiLienLac, SoDienThoai, LoaiKhachHang";
                sqlStatement += " FROM KhachHang";
                sqlStatement += " WHERE SoDienThoai = '" + strchuoiTimKiem + "'";
            }
            else if (timKiemTheo == 5)
            {
                sqlStatement = " SELECT MaKhachHang, TenKhachHang, DiaChiLienLac, SoDienThoai, LoaiKhachHang";
                sqlStatement += " FROM KhachHang";
                sqlStatement += " WHERE LoaiKhachHang like N'%" + strchuoiTimKiem + "%'";
            }
            openConnect();


            SqlCommand cmd = new SqlCommand(sqlStatement, con);
            danhSachKhachHangAdapter = new SqlDataAdapter(cmd);
            danhSachKhachHangAdapter.SelectCommand = cmd;
            SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(danhSachKhachHangAdapter);
            danhSachKhachHangAdapter.Fill(ds, "KhachHang");
            closeConnect();
            return ds;         
        }

       
    }
}
